<%
    /** 
     * 打印标题
     */
%>
<%
    logger.info("1. 构造标题头区");
    ItemTitleArea itArea = (ItemTitleArea)request.getAttribute("viewer");
    logger.debug("  -- 取标题头区");
    List titles = itArea.getTitles(request.getContextPath());
    List realTitles = new ArrayList();
    logger.debug("  -- 显示标题头");
    
    short index4Title = 0;
    if(titleSorts == null)
    {
        for(int i = 0; i < titles.size(); i ++)
        {
            Title title = (Title)titles.get(i);
            if(title.getPrinted())
            {
                realTitles.add(title);



            }
        }
    }
    else
    {
        for(int j = 0; j < titleSorts.length; j ++)
        {
            Title title = (Title)titles.get(Integer.parseInt(titleSorts[j]));
            if(title.getPrinted())
            {
                realTitles.add(title);
            }
        }
    }
    // 开始取最大深度和最大叶子数
    Title rootTitle = new Title();
    rootTitle.setSubTitles(realTitles);
    int maxDepth = rootTitle.getTreeDepth() - 1;
    int sumOfLeafs = rootTitle.getMaxSumOfSubTitlesLeafs();
    logger.debug("maxDepth: " + maxDepth);
    logger.debug("sumOfLeafs: " + sumOfLeafs);
    // 代码
    HSSFRow[] titleRows = new HSSFRow[maxDepth];
    int[] cellIndexs = new int[maxDepth];
    for(int i = 0; i < titleRows.length; i ++)
    {
        titleRows[i] = sheet.createRow((short) reportHeight + 2);
    }
    maxLength = this.makeTitleHtmlCode(wb,
                                       realTitles,
                                       titleRows, 
                                       sheet,
                                       maxLength,
                                       index4Title,
                                       maxDepth, 
                                       sumOfLeafs,
                                       (reportHeight + 2),
                                       cellIndexs);
    logger.info("outPrintTitles.jsp 完毕" + reportHeight);
%>

<%! // 构造树状标题头
    private int  makeTitleHtmlCode(HSSFWorkbook wb,
                                   List realTitles, // 标题树 集合
                                   HSSFRow[] titleRows, 
                                   HSSFSheet sheet,
                                   int maxLength,
                                   short index4Title,
                                   int maxDepth, // 最大树深
                                   int sumOfLeafs,// 最大叶子数，好像用不上
                                   int reportHeight,
                                   int[] cellIndexs
                                   )
    {
            // 开始打印标题
            for(int i = 0; i < realTitles.size(); i ++)
            {
                Title title = (Title)realTitles.get(i);  
                HSSFRow titleRow = titleRows[title.getTreeHighth()];
                int itsSumOfLeafs = title.getSumOfLeafs();
                if(itsSumOfLeafs == 0)
                    itsSumOfLeafs = 1;
                if(title.getTreeHighth() == 0)
                {
                    maxLength = maxLength + itsSumOfLeafs; // 记录显示多少个字段        
                }
                int rows = 0;
                if(title.getTreeHighth() > 0)
                {
                    if(title.getTreeDepth() > 0 )
                    {
                        rows = 0;
                    }
                    else
                    {
                        rows = maxDepth - title.getTreeHighth() -  1; 
                    }
                    
                }
                else
                {
                    if(title.getTreeDepth() > 0 )
                    {
                        rows = 0;
                    }
                    else
                    {
                        rows = maxDepth - title.getTreeHighth() - 1;
                    }
                    
                }
                // 每行
//                logger.debug("生成标题: " + title.getName());
                if(index4Title != 0)
				{
                    sheet.setColumnWidth((short)index4Title,(short)(title.getName().length() * 1000));  //一般的title用
				}
				else
				{
					sheet.setColumnWidth((short)index4Title,(short)(3 * 1000));     //总合计用，要不第0列就不见了
				}
                int cellIndex = cellIndexs[title.getTreeHighth()];
                logger.debug("cellIndexs[" + title.getTreeHighth() + "] = " + cellIndex);                
                for(int j = 0 ; j < (rows + 1); j ++)
                {
                    logger.debug("cellIndexs[" + (j + title.getTreeHighth()) + "] =  " + cellIndex + " + " + title.getSumOfLeafs() + " + 1;");
                    cellIndexs[j + title.getTreeHighth()] =  cellIndex + itsSumOfLeafs;
                }
                Region region1 = new Region((title.getTreeHighth() + reportHeight), //**
                                            (short)cellIndex,
                                            (short)(title.getTreeHighth() + rows + reportHeight),           //**
                                            (short)(cellIndex + itsSumOfLeafs - 1));
                logger.debug("生成标题: " + title.getName() 
                                          + " : (" 
                                          + (title.getTreeHighth() + reportHeight) + "," 
                                          + (short)cellIndex + "," 
                                          + (short)(title.getTreeHighth() + rows + reportHeight) + "," 
                                          + (short)(cellIndex + itsSumOfLeafs - 1) + ")  rows: " + rows);
                
                HSSFCellStyle titleStyle = wb.createCellStyle();
                titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 左右:居中
                titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下:居中
                titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
                titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
                titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
                titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 左右:居中
                titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下:居中
                HSSFFont titleFont = wb.createFont(); // 标题字体
                titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 大粗
                titleFont.setFontHeightInPoints((short)14); // 字体大小
                titleStyle.setFont(titleFont);
                this.fillMergedRegion(wb,
                                      sheet,
                                      region1,
                                      title.getName(),
                                      titleStyle);
                index4Title ++;                
                if(title.getSubTitles().size() > 0)
                {
                    this.makeTitleHtmlCode(wb,
                                           title.getSubTitles(),
                                           titleRows, 
                                           sheet,
                                           maxLength,
                                           index4Title,
                                           maxDepth, 
                                           sumOfLeafs,
                                           reportHeight,
                                           cellIndexs);
                }
            }// for
            return maxLength;
    }
%>
